package instruction

import (
	"gitee.com/microwww/jvm-go/jvm/class"
	"gitee.com/microwww/jvm-go/jvm/rtda"
)

func init() {
	class.PutInitStatic(initStatic)
	// class.AddClassListener("java/lang/System", javaLangSystem_OUT_IN)
}

func initStatic(clz *class.Class) {
	if clz.IsInitStaticVars() {
		return
	}
	clz.InitStaticVars()
	cl := clz.GetMethod("<clinit>", "()V")
	if cl == nil {
		return
	}
	th := rtda.NewThread(clz.ClassLoader())
	Instruct(th, cl)
}

//java/lang/System ()V
func javaLangSystem_OUT_IN(clazz *class.Class) {
	thread := rtda.NewThread(clazz.ClassLoader())
	{
		clz := clazz.ClassLoader().DefineClass("java/io/FileOutputStream")
		out := CreateObject(thread, clz, "(Ljava/lang/String;)V", nil)
		clazz.SetStaticVars("out", out)
	}
	{
		clz := clazz.ClassLoader().DefineClass("java/io/FileInputStream")
		in := CreateObject(thread, clz, "(Ljava/lang/String;)V", nil)
		clazz.SetStaticVars("in", in)
	}
}
